package com.tjec.project.projectLeaderWorkbeanch.controller;

import com.tjec.common.constant.Constants;
import com.tjec.common.utils.SecurityUtils;
import com.tjec.common.utils.StringUtils;
import com.tjec.framework.aspectj.lang.annotation.Log;
import com.tjec.framework.aspectj.lang.enums.BusinessType;
import com.tjec.framework.web.controller.BaseController;
import com.tjec.framework.web.domain.AjaxResult;
import com.tjec.framework.web.page.TableDataInfo;
import com.tjec.project.projectLeaderWorkbeanch.domain.BmsLeaderAdminHistory;
import com.tjec.project.projectLeaderWorkbeanch.domain.BmsLeaderMember;
import com.tjec.project.projectLeaderWorkbeanch.service.IBmsLeaderAdminHistoryService;
import com.tjec.project.projectLeaderWorkbeanch.service.IBmsLeaderMemberService;
import com.tjec.project.system.domain.SysRole;
import com.tjec.project.system.domain.SysUser;
import com.tjec.project.system.domain.SysUserRole;
import com.tjec.project.system.mapper.SysUserRoleMapper;
import com.tjec.project.system.service.ISysRoleService;
import com.tjec.project.system.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 管理员授权Controller
 *
 * @author cjj
 * @date 2020-05-18
 */
@Api(value = "项目负责人工作台-管理员授权" , tags = {"项目负责人工作台-管理员授权接口"})
@RestController
@RequestMapping("/projectLeader/adminAuthorize")
public class BmsAdminAuthorizeController extends BaseController {

    @Autowired
    private IBmsLeaderMemberService bmsLeaderMemberService;

    @Autowired
    private IBmsLeaderAdminHistoryService bmsLeaderAdminHistoryService;

    @Autowired
    private ISysRoleService sysRoleService;

    @Autowired
    private SysUserRoleMapper sysUserRoleMapper;

    @Autowired
    private ISysUserService sysUserService;

    /**
     * 查询个人专家列表
     */
    @ApiOperation("管理员授权列表")
//    @PreAuthorize("@ss.hasPermi('projectLeader:adminAuthorize:list')")
    @GetMapping("/list")
    public AjaxResult list(BmsLeaderMember bmsLeaderMember)
    {
       /* String projectLeaderNo = sysUserService.findProjectLeaderNoByUserGuid(bmsLeaderMember.getLeader());
        if(StringUtils.isNotBlank(projectLeaderNo)){
            bmsLeaderMember.setLeader(projectLeaderNo);
        }*/
        List<BmsLeaderMember> list = bmsLeaderMemberService.selectAdminAuthorizeList(bmsLeaderMember.getLeader());
        return AjaxResult.success(list);
    }
    
    /**
     * 查询代理人列表
     */
    @ApiOperation("代理人授权列表")
//    @PreAuthorize("@ss.hasPermi('projectLeader:adminAuthorize:list')")
    @GetMapping("/agencyList")
    public AjaxResult agencyList(BmsLeaderMember bmsLeaderMember)
    {
       /* String projectLeaderNo = sysUserService.findProjectLeaderNoByUserGuid(bmsLeaderMember.getLeader());
        if(StringUtils.isNotBlank(projectLeaderNo)){
            bmsLeaderMember.setLeader(projectLeaderNo);
        }*/
        List<BmsLeaderMember> list = bmsLeaderMemberService.selectAgencyList(bmsLeaderMember.getLeader());
        return AjaxResult.success(list);
    }

    /**
     * 查询个人专家列表
     */
    @ApiOperation("管理的项目负责人列表")
//    @PreAuthorize("@ss.hasPermi('projectLeader:adminAuthorize:list')")
    @GetMapping("/leaderList")
    public AjaxResult leaderList(BmsLeaderMember bmsLeaderMember)
    {
        List<BmsLeaderMember> list = bmsLeaderMemberService.selectBmsLeaderMemberLeaderList(bmsLeaderMember);
        if(StringUtils.isNotBlank(bmsLeaderMember.getUserId())&&sysRoleService.isExistRoleByUserId(Constants.ROLE_PROJECT_LEADER_KEY, bmsLeaderMember.getUserId())){
            BmsLeaderMember leaderMember = new BmsLeaderMember();
            leaderMember.setUserId(bmsLeaderMember.getUserId());
            SysUser sysUser = sysUserService.selectUserById(bmsLeaderMember.getUserId());
            leaderMember.setNickName(sysUser.getNickName());
            leaderMember.setLeader(bmsLeaderMember.getUserId());
            leaderMember.setJobNo(sysUser.getJobNo());
            list.add(leaderMember);
        }
        return AjaxResult.success(list);
    }

    /**
     * 查询管理员变更历史记录列表
     */
    @ApiOperation("查询管理员变更历史记录列表")
//    @PreAuthorize("@ss.hasPermi('projectLeader:adminAuthorize:historyList')")
    @GetMapping("/historyList")
    public TableDataInfo historyList(BmsLeaderAdminHistory bmsLeaderAdminHistory)
    {
        startPage();
        /*String projectLeaderNo = sysUserService.findProjectLeaderNoByUserGuid(bmsLeaderAdminHistory.getLeader());
        if(StringUtils.isNotBlank(projectLeaderNo)){
            bmsLeaderAdminHistory.setLeader(projectLeaderNo);
        }*/
        List<BmsLeaderAdminHistory> list = bmsLeaderAdminHistoryService.selectBmsLeaderAdminHistoryList(bmsLeaderAdminHistory);
        return getDataTable(list);
    }

    /**
     * 取消当前管理员
     */
    @Transactional
    @ApiOperation("取消当前管理员")
    @ApiImplicitParam(name = "bmsLeaderMember", value = "取消当前管理员", dataType = "BmsLeaderMember")
    @Log(title = "取消当前管理员", businessType = BusinessType.UPDATE)
//    @PreAuthorize("@ss.hasPermi('projectLeader:adminAuthorize:remove')")
    @PutMapping("/cancel")
    public AjaxResult cancel(@RequestBody BmsLeaderMember bmsLeaderMember)
    {
        if(StringUtils.isBlank(bmsLeaderMember.getGuid())){
            AjaxResult.error("数据唯一guid为空");
        }
        /*String projectLeaderNo = sysUserService.findProjectLeaderNoByUserGuid(bmsLeaderMember.getLeader());
        if(StringUtils.isNotBlank(projectLeaderNo)){
            bmsLeaderMember.setLeader(projectLeaderNo);
        }*/
        bmsLeaderMember.setRole("0");
        BmsLeaderAdminHistory bmsLeaderAdminHistory = new BmsLeaderAdminHistory();
        bmsLeaderAdminHistory.setLeader(bmsLeaderMember.getLeader());
        bmsLeaderAdminHistory.setUserId(bmsLeaderMember.getUserId());
        bmsLeaderAdminHistory.setRemark("取消管理员");
        bmsLeaderAdminHistoryService.insertBmsLeaderAdminHistory(bmsLeaderAdminHistory);
        BmsLeaderMember member = new BmsLeaderMember();
        member.setUserId(bmsLeaderMember.getUserId());
        member.setRole("1");
        List<BmsLeaderMember> bmsLeaderMembers = bmsLeaderMemberService.selectBmsLeaderMemberList(member);
        //刪除角色前先判断是否存在其他项目负责人授权为项目负责人工作台管理员
        if(CollectionUtils.isEmpty(bmsLeaderMembers)||bmsLeaderMembers.size()==1){
            //用户刪除角色为项目负责人工作台管理员
            SysRole role = new SysRole();
            role.setRoleKey(Constants.ROLE_PROJECT_LEADER_WORKBENCH_ADMIN_KEY);
            String roleId = sysRoleService.selectRoleList(role).get(0).getRoleId();
            SysUserRole sysUserRole = new SysUserRole();
            sysUserRole.setRoleId(roleId);
            sysUserRole.setUserId(bmsLeaderMember.getUserId());
            sysUserRoleMapper.deleteUserRoleInfo(sysUserRole);
        }
        return toAjax(bmsLeaderMemberService.updateBmsLeaderMember(bmsLeaderMember));
    }

    /**
     * 添加当前管理员
     */
    @Transactional
    @ApiOperation("添加当前管理员")
    @ApiImplicitParam(name = "bmsLeaderMember", value = "取消当前管理员", dataType = "BmsLeaderMember")
    @Log(title = "添加当前管理员", businessType = BusinessType.UPDATE)
//    @PreAuthorize("@ss.hasPermi('projectLeader:adminAuthorize:add')")
    @PutMapping("/add")
    public AjaxResult add(@RequestBody BmsLeaderMember bmsLeaderMember)
    {
        if(StringUtils.isBlank(bmsLeaderMember.getGuid())){
            AjaxResult.error("数据唯一guid为空");
        }
        /*String projectLeaderNo = sysUserService.findProjectLeaderNoByUserGuid(bmsLeaderMember.getLeader());
        if(StringUtils.isNotBlank(projectLeaderNo)){
            bmsLeaderMember.setLeader(projectLeaderNo);
        }*/
        bmsLeaderMember.setRole("1");
        BmsLeaderAdminHistory bmsLeaderAdminHistory = new BmsLeaderAdminHistory();
        bmsLeaderAdminHistory.setLeader(bmsLeaderMember.getLeader());
        bmsLeaderAdminHistory.setUserId(bmsLeaderMember.getUserId());
        bmsLeaderAdminHistory.setRemark("授权管理员");
        bmsLeaderAdminHistoryService.insertBmsLeaderAdminHistory(bmsLeaderAdminHistory);
        //用户设置角色为项目负责人工作台管理员
        SysRole role = new SysRole();
        role.setRoleKey(Constants.ROLE_PROJECT_LEADER_WORKBENCH_ADMIN_KEY);
        String roleId = sysRoleService.selectRoleList(role).get(0).getRoleId();
        SysUserRole sysUserRole = new SysUserRole();
        sysUserRole.setRoleId(roleId);
        sysUserRole.setUserId(bmsLeaderMember.getUserId());
        List<SysUserRole> SysUserRoles = sysUserRoleMapper.selectUserRoleList(sysUserRole);
        if(CollectionUtils.isEmpty(SysUserRoles)){
            List<SysUserRole> sysUserRoles = new ArrayList<>();
            sysUserRoles.add(sysUserRole);
            sysUserRoleMapper.batchUserRole(sysUserRoles);
        }
        return toAjax(bmsLeaderMemberService.updateBmsLeaderMember(bmsLeaderMember));
    }
}
